package com.atguigu.gmall.user.service.impl;

import com.atguigu.gmall.model.user.UserInfo;
import com.atguigu.gmall.user.mapper.UserInfoMapper;
import com.atguigu.gmall.user.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public UserInfo login(UserInfo userInfo) {
        // 用户登录 要验证用户名、密码是否和数据库的匹配
        // select * from user_info where login_name = ? and passwd = ?

        String passwd = userInfo.getPasswd(); // 用户输入的是明文 数据库是密文
        String newPassword = DigestUtils.md5DigestAsHex(passwd.getBytes());// MD5 加密

        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("login_name",userInfo.getLoginName());
        wrapper.eq("passwd", newPassword);

        // 获取用户信息
        UserInfo loginInfo = userInfoMapper.selectOne(wrapper);

        // 判断用户是否存在
        if (null != loginInfo){
            return loginInfo;
        }
        return null;
    }
}
